/**
 * 获取弧度
 *
 * @param   {number}  degrees  [传入角度]
 *
 * @return  {number}           [返回弧长]
 */
function getRadian (degrees) {
  return (Math.PI / 180) * degrees
}

/**
 * 获取度数
 *
 * @param   {number}  radian  [传入弧长]
 *
 * @return  {number}          [返回度数]
 */
function getDegrees (radian) {
  return (180 / Math.PI) * radian
}

// 获取圆边上点的坐标
function getRadiusPoint (
  x,
  y,
  radius,
  degrees
) {
  return {
    x1: x + radius * Math.cos(degrees),
    y1: y + radius * Math.sin(degrees)
  }
}

/**
 * [isObject 判断是否为一个对象]
 *
 * @param   {[type]}  item  [传入的值]
 *
 * @return  {[type]}        [返回布尔值]
 */
function isObject (item) {
  return item && typeof item === 'object' && !Array.isArray(item)
}

/**
 * [mergeDeep description]
 *
 * @param   {[type]}  target:      [target: 被合并的对象]
 * @param   {[type]}  ...sources:  [...sources: 合并的对象]
 *
 * @return  {any}                  [return 深合并后的对象]
 */
function mergeDeep (target, ...sources) {
  if (!sources.length) return target
  const source = sources.shift()

  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      if (isObject(source[key])) {
        if (!target[key]) Object.assign(target, { [key]: {} })
        mergeDeep(target[key], source[key])
      } else {
        Object.assign(target, { [key]: source[key] })
      }
    }
  }

  return mergeDeep(target, ...sources)
}

/**
 * 根据分数自动定义信用等级
 */
function getScoreLevelStyle (actualScore) {
  const score = actualScore
  const styleObj = {}
  if (score < 500) {
    styleObj.color = '#FF8900'
    styleObj.rgbColorArr = [255, 137, 0]
    styleObj.rgbColor = styleObj.rgbColorArr.join(', ')
  } else if (score < 750) {
    styleObj.color = '#128BED'
    styleObj.rgbColorArr = [18, 139, 237]
    styleObj.rgbColor = styleObj.rgbColorArr.join(', ')
  } else {
    styleObj.color = '#00AD65'
    styleObj.rgbColorArr = [0, 173, 101]
    styleObj.rgbColor = styleObj.rgbColorArr.join(', ')
  }

  return styleObj
}

/**
 * 根据得分获取星星数量
 *
 * @param {*} score 得分
 */
const getStarsByScore = (score) => {
  /** 均分10等分：最大10颗半星即满5星 */
  const allHalfStars = Math.floor(score / 10) + 1
  /** 满星数 */
  const stars = Math.floor(allHalfStars / 2)
  /** 半星数 */
  const halfStar = allHalfStars % 2

  const arr = []
  for (let i = 0; i < 5; i++) {
    if (i + 1 <= stars) {
      arr.push(1)
    } else if (i === stars && halfStar) {
      arr.push(0.5)
    } else {
      arr.push(0)
    }
  }

  return arr
}

const accAdd = (arg1, arg2) => {
  let r1 = 0
  let r2 = 0
  const getArr = (f) => f.toString().split('.')
  if (getArr(arg1).length > 1) {
    r1 = getArr(arg1)[1].length
  }
  if (getArr(arg2).length > 1) {
    r2 = getArr(arg2)[1].length
  }

  const m = Math.pow(10, Math.max(r1, r2))
  return (Math.round(arg1 * m) + Math.round(arg2 * m)) / m
}

const accSub = (arg1, arg2) => {
  let r1 = 0
  let r2 = 0
  const getArr = (f) => f.toString().split('.')
  if (getArr(arg1).length > 1) {
    r1 = getArr(arg1)[1].length
  }
  if (getArr(arg2).length > 1) {
    r2 = getArr(arg2)[1].length
  }

  const m = Math.pow(10, Math.max(r1, r2))
  return (Math.round(arg1 * m) - Math.round(arg2 * m)) / m
}

const accMul = (num1, num2) => {
  const s1 = num1.toString()
  const s2 = num2.toString()
  let m = 0
  const d1 = s1.split('.')
  const d2 = s2.split('.')
  if (d1.length > 1) {
    m += d1[1].length
  }
  if (d2.length > 1) {
    m += d2[1].length
  }

  return (s1.replace('.', '') - 0) * (s2.replace('.', '') - 0) / Math.pow(10, m)
}

const accDiv = (num1, num2) => {
  let t1 = 0
  let t2 = 0
  let r1 = 0
  let r2 = 0
  const d1 = num1.toString().split('.')
  const d2 = num2.toString().split('.')

  if (d1.length > 1) {
    t1 = d1[1].length
  }
  if (d2.length > 1) {
    t2 = d2[1].length
  }
  r1 = Number(num1.toString().replace('.', ''))
  r2 = Number(num2.toString().replace('.', ''))
  return (r1 / r2) * Math.pow(10, t2 - t1)
}

const scoreList = [
  {
    percent: '25%',
    min: 0,
    max: 499,
    list: [
      { name: 'L-1', min: 0, max: 299, opacity: 0.5, showText: 'min' },
      { name: 'L-2', min: 300, max: 399, opacity: 0.4 },
      { name: 'L-3', min: 400, max: 499, opacity: 0.3 }
    ]
  },
  {
    percent: '42%',
    min: 500,
    max: 749,
    list: [
      { name: 'L-4', min: 500, max: 549, opacity: 0.2, showText: 'min' },
      { name: 'L-5', min: 550, max: 599, opacity: 0.3 },
      { name: 'L-6', min: 600, max: 649, opacity: 0.4, showText: 'min' },
      { name: 'L-7', min: 650, max: 699, opacity: 0.5 },
      { name: 'L-8', min: 700, max: 749, opacity: 0.6 }
    ]
  },
  {
    percent: '33%',
    min: 750,
    max: 2000,
    list: [
      { name: 'L-9', min: 750, max: 799, opacity: 0.2, showText: 'min' },
      { name: 'L-10', min: 800, max: 899, opacity: 0.3 },
      { name: 'L-11', min: 900, max: 999, opacity: 0.4, showText: 'min' },
      { name: 'L-12', min: 1000, max: 2000, opacity: 0.5, showText: 'max' }
    ]
  }
]


const setScorePosi = (score) => {
  let part = 0
  if (score) {
    for(let el of scoreList) {

      const percentFloat = Number(`0.` + _.parseInt(el.percent))
      if (score > el.max) {
        part = accAdd(part, percentFloat)
      } else if (score >= el.min && score <= el.max) {
        const target = _.find(el.list, v => score >= v.min && score <= v.max)
        const index = _.findIndex(el.list, v => score >= v.min && score <= v.max)
        const subPart = accMul(accDiv(index, el.list.length), percentFloat)
        const posi = accDiv(accSub(score, target.min), accSub(target.max + 1, target.min))
        const thirdPart = accMul(accDiv(1, el.list.length), percentFloat)
        part = accAdd(part, accAdd(subPart, accMul(thirdPart, posi)))
      }

    }

  }

  return accMul(part, 100).toFixed(2) + '%'
}